package com.lcxsoft.userservice.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.impl.CollectionConverter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lcxsoft.commonutils.JwtUtils;
import com.lcxsoft.commonutils.R;
import com.lcxsoft.servicebase.exceptionhandler.FoodboxException;
import com.lcxsoft.userservice.entity.CustomUser;
import com.lcxsoft.userservice.query.UpdateUserInfoQuery;
import com.lcxsoft.userservice.query.UserQuery;
import com.lcxsoft.userservice.service.CustomUserService;
import com.lcxsoft.userservice.url.UserServiceURL;
import com.mysql.cj.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-01-12
 */
@RestController
@RequestMapping(UserServiceURL.packageUrl)
public class CustomUserController {

    @Resource
    private CustomUserService customUserService;


    /**
     * 登录
     * @param user
     * @return
     */
    @RequestMapping(value = UserServiceURL.CustomerUserUrl.login, method = RequestMethod.POST)
    public R login(@RequestBody UserQuery user){
        List<String> nameList = new ArrayList<>();
        nameList.add("张三");
        nameList.add("李四");
        nameList.add("王五");
        nameList.add("赵六");
        nameList.add("钱七");
        nameList.add("孙八");
        List<String> collect = nameList.stream().filter(a -> "三".equals(a)).collect(Collectors.toList());
        System.out.println(collect);


        String token = customUserService.login(user.getUserPhone(), user.getUserPassword());
        return R.ok().data("token", token);
    }

    public static void main(String[] args) throws Exception {
        List<String> nameList = new ArrayList<>();
        nameList.add("张三");
        nameList.add("李四");
        nameList.add("王五");
        nameList.add("赵六");
        nameList.add("钱七");
        nameList.add("孙八");
        List<String> collect = nameList.stream().filter(a -> "张三".equals(a)).collect(Collectors.toList());
    }

    /**
     * 注册
     * @param query
     * @return
     */
    @RequestMapping(value = UserServiceURL.CustomerUserUrl.register, method = RequestMethod.POST)
    public R register(UserQuery query){
        return customUserService.register(query);
    }

    /**
     * 获取token
     * @param request
     * @return
     */
    @RequestMapping(value = UserServiceURL.CustomerUserUrl.userInfo, method = RequestMethod.POST)
    public R userInfo(HttpServletRequest request){
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isNullOrEmpty(userId)){
            return R.error().message("认证信息有误");
        }
        CustomUser user = customUserService.getById(userId);
        return R.ok().data("userInfo", user);
    }

    /**
     * 修改用户信息
     * @param query
     * @return
     */
    @RequestMapping(value = UserServiceURL.CustomerUserUrl.updateUserInfo, method = RequestMethod.POST)
    public R updateUserInfo(UpdateUserInfoQuery query){
        // TODO --修改用户信息 待完成
        return null;
    }

    /**
     * 根据用户ID充值
     * @param tottleAmount
     * @param userId
     * @return
     */
    @RequestMapping(value = UserServiceURL.CustomerUserUrl.chargeById, method = RequestMethod.POST)
    public R chargeById(BigDecimal tottleAmount, String userId){
        CustomUser user = customUserService.getOne(new LambdaQueryWrapper<CustomUser>().eq(CustomUser::getUserId, userId));
        user.setUserBalance(user.getUserBalance().add(tottleAmount));
        user.setUpdatetime(LocalDateTime.now());
        boolean b = customUserService.updateById(user);
        return b ? R.ok().message("充值成功") : R.error().message("充值失败");
    }

    /**
     * 根据UserI的获取User对象
     * @param userId
     * @return
     */
    @RequestMapping(value = UserServiceURL.CustomerUserUrl.queryUserByUserId, method = RequestMethod.POST)
    public CustomUser queryUserByUserId(@RequestBody String userId){
        return customUserService.getOne(new LambdaQueryWrapper<CustomUser>().eq(CustomUser::getUserId, userId));
    }

    /**
     * 查询用户列表
     * @param query
     * @return
     */
    @RequestMapping(value = UserServiceURL.CustomerUserUrl.queryUserList, method = RequestMethod.POST)
    public R queryUserList(UserQuery query){
        return customUserService.findUserList(query);
    }

    /**
     * 今日新增用户
     * @return
     */
    @PostMapping(UserServiceURL.CustomerUserUrl.todayNewRegister)
    public R todayNewRegister(){
        return customUserService.todayNewRegister();
    }

    /**
     * 总用户
     * @return
     */
    @PostMapping(UserServiceURL.CustomerUserUrl.totalRegister)
    public R totalRegister(){
        return R.ok().data("totalUser", customUserService.count(null));
    }



}

